package com.alxy.day1.controller;

import com.alxy.day1.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 爱莉希雅
 * @version java 17
 */
@SuppressWarnings({"all"})
@RestController
public class ProductController {
    @Autowired
    private ProductService productService;

    // 乐观锁购买商品接口
    @PostMapping("/products/{id}/purchase/optimistic")
    public boolean purchaseOptimistically(@PathVariable Long id, @RequestParam int quantity) {
        return productService.purchaseProductOptimistically(id, quantity);
    }

    // 悲观锁购买商品接口
    @PostMapping("/products/{id}/purchase/pessimistic")
    public boolean purchasePessimistically(@PathVariable Long id, @RequestParam int quantity) {
        return productService.purchaseProductPessimistically(id, quantity);
    }
}
